package menu
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.NumericStepper;
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author robt
	 */
	public class MenuPopup extends Sprite
	{
		[Embed(source="../assets/assets.swf", symbol="MenuPopup")]
		protected var AssetClass : Class;
		private var _menu : Sprite;
		private var _imageURLInputText : InputText;
		private var _imageURLPushButton : PushButton;
		private var _main : Main;
		private var _mineRadio : RadioButton;
		private var _pickupRadio : RadioButton;
		private var _drawDamageRadio : RadioButton;
		private var _drawNoDamageRadio : RadioButton;
		private var _generateButton : PushButton;
		private var _generateTileMapButton : PushButton;
		private var _levelInputText : NumericStepper;
		private var _attractorRadio : RadioButton;
		private var _repellerRadio : RadioButton;
		private var _reactorRadio : RadioButton;
		private var _laserRadio : RadioButton;
		private var _laserSizeInputText : NumericStepper;
		private var _laserHorizontalCheck : CheckBox;
		private var _sizeInputText : NumericStepper;
		private var _viewCodeButton : PushButton;
		private var _hatchSizeInputText : NumericStepper;
		private var _hatchRadio : RadioButton;
		private var _reactorDoorRadio : RadioButton;
		private var _reactorDoorSizeInputText : NumericStepper;
		private var _reactorDoorHorizontalCheck : CheckBox;
		private var _reactorDoorOpenCheck : CheckBox;
		
		private var _switchDoorRadio : RadioButton;
		private var _switchDoorSizeInputText : NumericStepper;
		private var _switchDoorHorizontalCheck : CheckBox;
		private var _switchDoorOpenCheck : CheckBox;
		private var _switchDoorTopLeftCheck : CheckBox;
		private var _reactorGravityRadio : RadioButton;
		private var _laserOnInputText : NumericStepper;
		private var _laserOffInputText : NumericStepper;
		private var _discretePushButton : PushButton;
		private var _zoneInputText : NumericStepper;

		public function MenuPopup(main : Main)
		{
			_main = main;
			_menu = new AssetClass();
			addChild(_menu);

			var laserRotation : Sprite = new Sprite();
			addChild(laserRotation);
			
			var reactorDoorRotation : Sprite = new Sprite();
			addChild(reactorDoorRotation);

			_levelInputText = new NumericStepper(this, 10, 10, updateLevel);
			_levelInputText.minimum = 1;
			_levelInputText.maximum = 900;
			_levelInputText.step = 1;
			_levelInputText.value = 1;
			_levelInputText.width = 60;
			_levelInputText.height = 25;

			_zoneInputText = new NumericStepper(this, 10, 35, updateZone);
			_zoneInputText.minimum = 1;
			_zoneInputText.maximum = 900;
			_zoneInputText.step = 1;
			_zoneInputText.value = 1;
			_zoneInputText.width = 60;
			_zoneInputText.height = 25;

			_imageURLInputText = new InputText(this, 70, 10, "icon.png");
			_imageURLInputText.width = 150;
			_imageURLInputText.height = 25;

			_imageURLPushButton = new PushButton(this, 230, 10, "LOAD", onLoadClicked);
			_imageURLPushButton.width = 50;
			_imageURLPushButton.height = 25;

			_generateButton = new PushButton(this, 290, 10, "BUILD", onGenerateClicked);
			_generateButton.width = 50;
			_generateButton.height = 25;

			_generateTileMapButton = new PushButton(this, 490, 10, "TILE MAP", onGenerateTileMapClicked);
			_generateTileMapButton.width = 50;
			_generateTileMapButton.height = 25;

			_discretePushButton = new PushButton(this, 550, 10, "DISCRETE", onDiscrete);
			_discretePushButton.width = 50;
			_discretePushButton.height = 25;

			_viewCodeButton = new PushButton(this, 430, 10, "CODE", onCodeClicked);
			_viewCodeButton.width = 50;
			_viewCodeButton.height = 25;

			_sizeInputText = new NumericStepper(this, 360, 10, updateGrid);
			_sizeInputText.minimum = 1;
			_sizeInputText.maximum = 900;
			_sizeInputText.step = 4;
			_sizeInputText.value = 8;
			_sizeInputText.width = 60;
			_sizeInputText.height = 25;

			// _sizeButton = new PushButton(this, 430, 10, "UPDATE", onGenerateClicked);
			// _generateButton.width = 50;
			// _generateButton.height = 25;

			_drawDamageRadio = new RadioButton(this, 10, 45, "DRAW DAMAGE", false, updateState);
			_drawNoDamageRadio = new RadioButton(this, 10, 60, "DRAW NO DAMAGE", true, updateState);
			_pickupRadio = new RadioButton(this, 10, 75, "PICKIP", false, updateState);
			_mineRadio = new RadioButton(this, 10, 90, "MINE", false, updateState);
			_attractorRadio = new RadioButton(this, 10, 120, "ATTRACTOR", false, updateState);
			_repellerRadio = new RadioButton(this, 10, 135, "REPELLER", false, updateState);
			_reactorRadio = new RadioButton(this, 10, 150, "REACTOR", false, updateState);
			_laserRadio = new RadioButton(this, 10, 165, "LASER", false, updateState);
			_hatchRadio = new RadioButton(this, 10, 195, "HATCH", false, updateState);
			_reactorDoorRadio = new RadioButton(this, 10, 210, "R-DOOR", false, updateState);
			_switchDoorRadio = new RadioButton(this, 10, 225, "S-DOOR", false, updateState);
			_reactorGravityRadio = new RadioButton(this, 10, 240, "REACTOR-G", false, updateState);

			_laserSizeInputText = new NumericStepper(this, 80, 165, updateState);
			_laserSizeInputText.minimum = 1;
			_laserSizeInputText.maximum = 900;
			_laserSizeInputText.step = 8;
			_laserSizeInputText.value = 32;
			_laserSizeInputText.width = 60;
			_laserSizeInputText.height = 25;

			_laserHorizontalCheck = new CheckBox(laserRotation, 150, 165, "HORIZONTAL", updateState);
			
			_laserOnInputText = new NumericStepper(this, 250, 165, updateState);
			_laserOnInputText.minimum = 1;
			_laserOnInputText.maximum = 900;
			_laserOnInputText.step = 1;
			_laserOnInputText.value = 5;
			_laserOnInputText.width = 60;
			_laserOnInputText.height = 25;
			
			_laserOffInputText = new NumericStepper(this, 350, 165, updateState);
			_laserOffInputText.minimum = 1;
			_laserOffInputText.maximum = 900;
			_laserOffInputText.step = 1;
			_laserOffInputText.value = 5;
			_laserOffInputText.width = 60;
			_laserOffInputText.height = 25;

			// Hatch
			_hatchSizeInputText = new NumericStepper(this, 80, 195, updateState);
			_hatchSizeInputText.minimum = 1;
			_hatchSizeInputText.maximum = 900;
			_hatchSizeInputText.step = 8;
			_hatchSizeInputText.value = 64;
			_hatchSizeInputText.width = 60;
			_hatchSizeInputText.height = 25;

			// Reactor door

			_reactorDoorSizeInputText = new NumericStepper(this, 80, 210, updateState);
			_reactorDoorSizeInputText.minimum = 1;
			_reactorDoorSizeInputText.maximum = 900;
			_reactorDoorSizeInputText.step = 8;
			_reactorDoorSizeInputText.value = 64;
			_reactorDoorSizeInputText.width = 60;
			_reactorDoorSizeInputText.height = 25;

			_reactorDoorHorizontalCheck = new CheckBox(reactorDoorRotation, 150, 210, "HORIZONTAL", updateState);
			_reactorDoorOpenCheck = new CheckBox(reactorDoorRotation, 250, 210, "OPEN", updateState);

			// Switch door

			_switchDoorSizeInputText = new NumericStepper(this, 80, 225, updateState);
			_switchDoorSizeInputText.minimum = 1;
			_switchDoorSizeInputText.maximum = 900;
			_switchDoorSizeInputText.step = 8;
			_switchDoorSizeInputText.value = 64;
			_switchDoorSizeInputText.width = 60;
			_switchDoorSizeInputText.height = 25;
 
			_switchDoorHorizontalCheck = new CheckBox(this, 150, 225, "HORIZONTAL", updateState);
			_switchDoorOpenCheck = new CheckBox(this, 250, 225, "OPEN", updateState);
			_switchDoorTopLeftCheck = new CheckBox(this, 350, 225, "TOP LEFT", updateState);

			// hide();
			updateLevelDisplay();
			updateGridDisplay();

			hide();
		}

		private function updateZone(event : Event) : void
		{
			States.zone = _levelInputText.value;
		}

		private function onDiscrete(event:Event) : void
		{
			_main.toggleDiscreet();
		}

		private function onGenerateTileMapClicked(event:Event) : void
		{
			_main.generateTileMap();
		}

		private function onCodeClicked(event : Event) : void
		{
			_main.viewCode();
		}

		private function updateLevel(event : Event) : void
		{
			updateLevelDisplay();
		}

		private function updateLevelDisplay() : void
		{
			States.level = _levelInputText.value;
			_imageURLInputText.text = "Level" + States.level + ".png";
		}

		private function updateGrid(event : Event) : void
		{
			updateGridDisplay();
		}

		private function updateGridDisplay() : void
		{
			States.gridSize = Number(_sizeInputText.value);
			_main.updateGrid();
		}

		private function onGenerateClicked(event : Event) : void
		{
			_main.generate();
		}

		private function updateState(event : Event) : void
		{
			if (_drawDamageRadio.selected)
			{
				States.state = States.DRAW_DAMAGE;
			}
			else if (_drawNoDamageRadio.selected)
			{
				States.state = States.DRAW_NO_DAMAGE;
			}
			else if (_pickupRadio.selected)
			{
				States.state = States.PICK_UP;
			}
			else if (_mineRadio.selected)
			{
				States.state = States.MINE;
			}
			else if (_reactorRadio.selected)
			{
				States.state = States.REACTOR;
			}
			else if (_attractorRadio.selected)
			{
				States.state = States.ATTRACTOR;
			}
			else if (_repellerRadio.selected)
			{
				States.state = States.REPELLER;
			}
			else if (_laserRadio.selected)
			{
				States.state = States.LASER;
			}
			else if (_hatchRadio.selected)
			{
				States.state = States.HATCH;
			}
			else if (_reactorDoorRadio.selected)
			{
				States.state = States.REACTOR_DOOR;
			}
			else if (_switchDoorRadio.selected)
			{
				States.state = States.SWITCH_DOOR;
			}
			else if (_reactorGravityRadio.selected)
			{
				States.state = States.REACTOR_GRAVITY;
			}

			States.gridSize = Number(_sizeInputText.value);
			States.laserWidth = Number(_laserSizeInputText.value);
			States.laserHorizontal = _laserHorizontalCheck.selected;
			States.laserOn = _laserOnInputText.value;
			States.laserOff = _laserOffInputText.value;
			States.reactorDoorWidth = Number(_reactorDoorSizeInputText.value);
			States.reactorDoorHorizontal = _reactorDoorHorizontalCheck.selected;
			States.reactorDoorOpen = _reactorDoorOpenCheck.selected;
			States.hatchSize = Number(_hatchSizeInputText.value);
			States.switchDoorWidth = Number(_switchDoorSizeInputText.value);
			States.switchDoorHorizontal = _switchDoorHorizontalCheck.selected;
			States.switchDoorOpen = _switchDoorOpenCheck.selected;
			States.switchDoorTopLeft = _switchDoorTopLeftCheck.selected;
		}

		private function onLoadClicked(event : Event) : void
		{
			_main.loadImage(_imageURLInputText.text);
		}

		public function show() : void
		{
			visible = true;
		}

		public function hide() : void
		{
			visible = false;
		}

		public function getLevel() : String
		{
			return String(_levelInputText.value);
		}

		public function getZone() : String
		{
			return String(_zoneInputText.value);
		}
	}
}
